/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ucb.ihm.view;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author gabriel
 */
@WebServlet(urlPatterns="/vote")
public class EnqueteServlet extends HttpServlet {

    public static final int[] votos = new int[5];
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String vote = req.getParameter("voto");
        
        req.getSession(true).setAttribute("votou", true);
        
        votos[Opcoes.valueOf(vote).index]++;
        
        req.setAttribute("votos", votos);
        req.setAttribute("total", getTotal());
        
        resp.sendRedirect(req.getContextPath());
        
    }
    
    public static int getTotal(){
        int total = 0;
        
        for (int i=0;i<5;i++){
            total += votos[i];
        }
        
        return total;
    }
    
    
    static enum Opcoes{
        CINCO_MESES(0), 
        DOZE_MESES(1), 
        TRES_ANOS(2), 
        MAIS_ANOS(3), 
        NAO_PRATICO(4);
        
        private final int index;

        private Opcoes(int index) {
            this.index = index;
        }

        public int getIndex() {
            return index;
        }
        
    };
    
}
